<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 模块控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\ModuleService;
use common\service\SchemaService;
use think\Request;

class ModuleController extends VerifyController
{

    /**
     * 添加模块（重写基类方法）
     * @param Request $request
     * @return array|\think\Response
     * @throws \Exception
     */
    public function create(Request $request)
    {
        $param = $request->param();
        $schemaService = SchemaService::getInstance();
        $resData = $schemaService->addModule($param['data']);
        return json(success_response('', $resData));
    }


    /**
     * 修改模块（重写基类方法）
     * @param Request $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $param = $request->param();
        $schemaService = SchemaService::getInstance();
        $resData = $schemaService->modifyModule($param['data']);
        return json(success_response('', $resData));
    }

    /**
     * 获取固定模块模块列表
     * @param Request $request
     * @return \think\Response
     */
    public function getFixedModuleListExcludeExist(Request $request)
    {
        $schemaService = SchemaService::getInstance();
        $resData = $schemaService->getFixedModuleListExcludeExist();
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 获取固定模块模块列表
     * @param Request $request
     * @return \think\Response
     */
    public function getEntityModuleList(Request $request)
    {
        $schemaService = SchemaService::getInstance();
        $resData = $schemaService->getEntityModuleList();
        return json(success_response('Query was successful.', $resData));
    }

    /**
     * 获取指定模块的关联模块
     * @param Request $request
     * @return \think\Response
     */
    public function getRelationModule(Request $request)
    {
        $param = $request->param();
        $schemaService = SchemaService::getInstance();
        $moduleData = $schemaService->getModuleRelationship($param['module_code']);
        return json(success_response('', $moduleData));
    }

    /**
     * 创建实体自定义模块
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function createEntitySchema(Request $request)
    {
        $param = $request->param();
        $moduleService = new ModuleService();
        $resData = $moduleService->createNewEntitySchema($param['data']);
        return json(success_response('', $resData));
    }

    /**
     * 删除实体自定义模块
     * @param Request $request
     * @return \think\Response
     */
    public function deleteEntityModule(Request $request)
    {
        $param = $request->param();
        $moduleService = new ModuleService();
        $resData = $moduleService->deleteEntityModule($param['param']['filter']['module_id']);
        return json(success_response('', $resData));
    }
}
